﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAKS.Models;
using SAKS.ViewModels;
using SAKS.IServices;
using SAKS.ActionFilters;
namespace SAKS.Controllers
{
    [NeedsPersistence]
    public class AwardController : ControllerBase
    {
        //
        // GET: /Award/
        public IAwardServices AwardServices { get; set; }
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            AwardServices.Session = GetCurrentSession(requestContext);
        }
        [Authorize(Roles = "Editor")]
        public ActionResult Index()
        {
            return View();
        }
        [Authorize(Roles = "Editor")]
        public ActionResult List(DataTableParameter param)
        {
            int? _start = param.iDisplayStart;
            int? _pageSize = param.iDisplayLength;
            int first = _start ?? 0;
            int pageSize = _pageSize ?? 20;
            int? totalRecords = new int?();
            var ret = AwardServices.List(first, pageSize, out totalRecords, param.sSearch);
            var json = Json(
                new dataTableJson
                {
                    sEcho = param.sEcho,
                    iTotalRecords = totalRecords,
                    iTotalDisplayRecords = totalRecords,
                    aaData = ret
                },
                JsonRequestBehavior.AllowGet
            );
            return json;
        }
        [HttpPost]
        [Authorize(Roles = "Editor")]
        public ActionResult Create(Award award)
        {
            AwardServices.Save(award);
            return RedirectToAction("Index");
        }
        [Authorize(Roles = "Editor")]
        public ActionResult Create()
        {
            return View();
        }
        [Authorize(Roles = "Editor")]
        public ActionResult Edit(Guid id)
        {
            return View(AwardServices.Get(id));
        }
        [HttpPost]
        [Authorize(Roles = "Editor")]
        public ActionResult Edit(Award award)
        {
            AwardServices.Update(award);
            return RedirectToAction("Index");
        }

        public ActionResult IsAwardNotExist(string fieldId, string fieldValue)
        {
            object[] result = new object[2];
            result[0] = fieldId;
            if (AwardServices.IsNameUsed(fieldValue))
            {
                result[1] = false;
            }
            else
                result[1] = true;
            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}
